﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace _4SquareLite
{
    public partial class frmCheckinResults : Form
    {
        string _strXML;

        public frmCheckinResults(string strXML)
        {
            InitializeComponent();
            Program.arrForm.Add(this);
            _strXML = strXML;
        }

        private void frmCheckinResults_Load(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            // read the xml
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(_strXML);
            XmlNode xmlRoot = xmlDocument.DocumentElement;

            //timestamp
            string strCreated = Program.getInnerText(xmlRoot, "created");
            if (!String.IsNullOrEmpty(strCreated))
            {
                lblTimestamp.Text = Program.convertAPITimeToLocalTime(strCreated);
            }
            
            // message
            lblMessage.Text = Program.getInnerText(xmlRoot, "message");

            ///////////////////////////////////////////////////////////////////////////
            // venue (may not exist)
            XmlNode xmlVenue = xmlRoot.SelectSingleNode("venue");
            if (xmlVenue != null)
            {
                XmlNode xmlCategory = xmlVenue.SelectSingleNode("primarycategory");
                if (xmlCategory != null)
                {
                    string strURL = Program.getInnerText(xmlCategory, "iconurl");
                    pbIcon.Image = Program.getImageFromURL(strURL);
                }

                lblVenueName.Text = Program.getInnerText(xmlVenue, "name");

                string strLabel = "";
                string strItem = "";

                //address
                strItem = Program.getInnerText(xmlVenue, "address");
                if (!String.IsNullOrEmpty(strItem))
                {
                    strLabel += strItem + " ";
                }

                //crossstreet
                strItem = Program.getInnerText(xmlVenue, "crossstreet");
                if (!String.IsNullOrEmpty(strItem))
                {
                    strLabel += strItem + " ";
                }

                //city
                strItem = Program.getInnerText(xmlVenue, "city");
                if (!String.IsNullOrEmpty(strItem))
                {
                    strLabel += strItem + " ";
                }

                //state
                strItem = Program.getInnerText(xmlVenue, "state");
                if (!String.IsNullOrEmpty(strItem))
                {
                    strLabel += strItem + " ";
                }

                //zip
                strItem = Program.getInnerText(xmlVenue, "zip");
                if (!String.IsNullOrEmpty(strItem))
                {
                    strLabel += strItem + " ";
                }

                lblAddress.Text = strLabel.Trim();

                pnlVenue.Visible = true;
            }
            else
            {
                pnlVenue.Visible = false;
                pnlVenue.Height = 0;
            }

            int y = pnlVenue.Location.Y + pnlVenue.Height + 3;

            ///////////////////////////////////////////////////////////////////////////
            // mayor (may not exist)
            XmlNode xmlMayor = xmlRoot.SelectSingleNode("mayor");
            if (xmlMayor != null)
            {
                string strMayorType = Program.getInnerText(xmlMayor, "type");
                switch (strMayorType)
                {
                    //node <type> which has the following values: 
                    //new (the user has been appointed mayorship), 
                    //nochange (the previous mayorship is still valid), 
                    //stolen (the user stole mayorship from the previous mayor)
                    case "new":
                        lblMayorType.Text = "You're the new Mayor!";
                        break;

                    case "stolen":
                        lblMayorType.Text = "You've overthrown the old Mayor!";
                        break;

                    case "nochange":
                        lblMayorType.Text = "Long live the Mayor!";
                        break;

                    default:
                        lblMayorType.Text = "";
                        break;
                }

                string strCheckins = Program.getInnerText(xmlMayor, "checkins");
                int iCheckins = int.Parse(strCheckins);
                if (iCheckins == 1)
                {
                    lblMayorCheckins.Text = iCheckins + " check-in here";
                }
                else
                {
                    lblMayorCheckins.Text = iCheckins + " check-ins here";
                }

                XmlNode xmlUser = xmlMayor.SelectSingleNode("user");
                if (xmlUser != null)
                {
                    string strName = Program.getInnerText(xmlUser, "firstname") + " " + Program.getInnerText(xmlUser, "lastname");
                    lblUserName.Text = strName.Trim();

                    string strGender = Program.getInnerText(xmlUser, "gender");
                    switch (strGender)
                    {
                        case "female":
                            pnlGender.BackColor = Color.PaleVioletRed;
                            break;

                        case "male":
                            pnlGender.BackColor = Color.SteelBlue;
                            break;

                        default:
                            pnlGender.BackColor = Color.Black;
                            break;
                    }

                    string strURL = Program.getInnerText(xmlUser, "photo");
                    pbUser.Image = Program.getImageFromURL(strURL);
                }
                else
                {
                    // collapse
                    pnlGender.Visible = false;
                    lblUserName.Visible = false;

                    lblMayorCheckins.Location = pnlGender.Location;
                    lblMayorCheckins.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
                    lblMayorCheckins.Width = pnlMayor.Width;

                    lblMayorType.Location = new Point(lblMayorCheckins.Location.X, lblMayorCheckins.Location.Y + lblMayorCheckins.Height);
                    lblMayorType.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
                    lblMayorType.Width = pnlMayor.Width;

                    lblMayorMessage.Location = new Point(lblMayorType.Location.X, lblMayorType.Location.Y + lblMayorType.Height);
                    lblMayorMessage.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
                    lblMayorMessage.Width = pnlMayor.Width;

                    pnlMayor.Height = lblMayorMessage.Location.Y + lblMayorMessage.Height;
                }

                string strMessage = Program.getInnerText(xmlMayor, "message");
                lblMayorMessage.Text = strMessage;
            }
            else
            {
                pnlMayor.Visible = false;
                pnlMayor.Height = 0;
            }

            pnlMayor.Location = new Point(3, y);
            y = pnlMayor.Location.Y + pnlMayor.Height + 3;
                
            ///////////////////////////////////////////////////////////////////////
            //badges (may not exist)
            int yBadge = lblBadges.Height + 3;
            XmlNodeList xmlBadges = xmlDocument.GetElementsByTagName("badge");
            foreach (XmlNode xmlBadge in xmlBadges)
            {
                ucBadgeListItem oBadge = new ucBadgeListItem(xmlBadge);
                oBadge.Location = new Point(0, yBadge);
                oBadge.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
                oBadge.Width = pnlBadges.Width - 3 * 2;

                yBadge += oBadge.Height + 3;
                pnlBadges.Controls.Add(oBadge);
                pnlBadges.Height = oBadge.Location.Y + oBadge.Height;
            }

            if (xmlBadges.Count == 0)
            {
                pnlBadges.Visible = false;
                pnlBadges.Height = 0;
            }

            pnlBadges.Location = new Point(3, y);
            y = pnlBadges.Location.Y + pnlBadges.Height + 3;

            ////////////////////////////////////////////////////////////////////////
            //scores (may not exist)
            int yScore = lblScores.Height + 3;
            XmlNodeList xmlScores = xmlDocument.GetElementsByTagName("score");
            foreach (XmlNode xmlScore in xmlScores)
            {
                ucScoreListItem oScore = new ucScoreListItem(xmlScore);
                oScore.Location = new Point(0, yScore);
                oScore.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
                oScore.Width = pnlScores.Width - 3 * 2;

                yScore += oScore.Height + 3;
                pnlScores.Controls.Add(oScore);
                pnlScores.Height = oScore.Location.Y + oScore.Height;
            }

            if (xmlScores.Count == 0)
            {
                pnlScores.Visible = false;
                pnlScores.Height = 0;
            }

            pnlScores.Location = new Point(3, y);
            y = pnlScores.Location.Y + pnlScores.Height + 3;

            //////////////////////////////////////////////////////////////////////////
            //specials (may not exist)
            int ySpecial = lblSpecials.Height + 3;
            XmlNodeList xmlSpecials = xmlDocument.GetElementsByTagName("special");
            foreach (XmlNode xmlSpecial in xmlSpecials)
            {
                XmlNode xmlThisVenue = xmlSpecial.SelectSingleNode("venue");
                if (xmlThisVenue != null)
                {
                    ucVenueListItem oVenue = new ucVenueListItem(xmlThisVenue);
                    oVenue.Location = new Point(0, ySpecial);
                    oVenue.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
                    oVenue.Width = pnlSpecials.Width;

                    ySpecial += oVenue.Height;
                    pnlSpecials.Controls.Add(oVenue);
                }

                ucSpecialListItem oSpecial = new ucSpecialListItem(xmlSpecial);
                oSpecial.Location = new Point(0, ySpecial);
                oSpecial.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
                oSpecial.Width = pnlSpecials.Width;

                ySpecial += oSpecial.Height + 3;
                pnlSpecials.Controls.Add(oSpecial);
                pnlSpecials.Height = oSpecial.Location.Y + oSpecial.Height;

                ySpecial += 3;
            }

            if (xmlSpecials.Count == 0)
            {
                pnlSpecials.Visible = false;
                pnlSpecials.Height = 0;
            }

            pnlSpecials.Location = new Point(3, y);
            y = pnlSpecials.Location.Y + pnlSpecials.Height;

            Cursor.Current = Cursors.Default;
        }

        private void inputPanel1_EnabledChanged(object sender, EventArgs e)
        {
            if (inputPanel1.Enabled)
            {
                pnlForm.Height -= inputPanel1.Bounds.Height;
            }
            else
            {
                pnlForm.Height = this.Height;
            }
        }

        private void miBack_Click(object sender, EventArgs e)
        {
            int i = Program.arrForm.Count;
            i--;
            Program.arrForm.RemoveAt(i);
            i--;
            ((Form)Program.arrForm[i]).Show();
            this.Close();
        }

        private void pbUser_Click(object sender, EventArgs e)
        {

        }
    }
}